package gui.advertiser;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.TitledBorder;

import application.ArenaManager;
import application.config.Config;
import domain.advertisement.Ad;

public class EditAdvertisementGUI extends JPanel {

	// Components:
	private JLabel lblIcon = null;

	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JTextField txtDescription;
	private JSpinner clickSpinner;
	private JSpinner viewsSpinner;
	private JSpinner durationSpinner;
	private JComboBox selectAdComboBox;
	// Data
	private Ad ad;
	private int adIndex;
	private Ad tmp;

	/**
	 * Create the panel.
	 */
	public EditAdvertisementGUI() {
		setVisible(true);

		setBorder(BorderFactory.createTitledBorder("Edit Ad"));
		setLayout(new BorderLayout(0, 0));

		JPanel btnPanel = new JPanel();
		add(btnPanel, BorderLayout.SOUTH);

		JButton btnSave = new JButton("Save");
		btnSave.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				editAdvertisement();
			}
		});
		btnPanel.add(btnSave);

		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ArenaManager.getInstance().startAdvertiser();
			}
		});
		btnPanel.add(btnCancel);

		JPanel iconPanel = new JPanel();
		add(iconPanel, BorderLayout.CENTER);
		iconPanel.setBorder(BorderFactory.createTitledBorder("Advertise"));
		iconPanel.setLayout(new BorderLayout(0, 0));

		txtDescription = new JTextField();
		txtDescription.setText("Description");
		iconPanel.add(txtDescription, BorderLayout.SOUTH);
		txtDescription.setColumns(10);
		txtDescription.setBorder(BorderFactory
				.createTitledBorder("Description"));

		lblIcon = new JLabel("");
		lblIcon.setIcon(null);
		iconPanel.add(lblIcon, BorderLayout.CENTER);

		JPanel schemePanel = new JPanel();
		schemePanel.setBorder(BorderFactory.createTitledBorder("Scheme"));
		add(schemePanel, BorderLayout.WEST);
		schemePanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		JPanel panel = new JPanel();
		schemePanel.add(panel);

		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "Viewable until", TitledBorder.LEADING, TitledBorder.TOP, null, null));

		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new TitledBorder(null, "Number of Clicks", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_2.setLayout(new BorderLayout(0, 0));

		JPanel panel_3 = new JPanel();
		panel_3.setBorder(new TitledBorder(null, "Number of Views", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_3.setLayout(new BorderLayout(0, 0));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
				gl_panel.createParallelGroup(GroupLayout.Alignment.TRAILING)
						.addGroup(GroupLayout.Alignment.LEADING, gl_panel.createSequentialGroup()
								.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
								.addContainerGap())
						.addGroup(gl_panel.createSequentialGroup()
								.addGroup(gl_panel.createParallelGroup(GroupLayout.Alignment.TRAILING)
										.addComponent(panel_3, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)
										.addComponent(panel_2, GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE))
								.addContainerGap())
				);
		gl_panel.setVerticalGroup(
				gl_panel.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
								.addComponent(panel_3, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)
								.addContainerGap(187, Short.MAX_VALUE))
				);

		viewsSpinner = new JSpinner();
		panel_3.add(viewsSpinner, BorderLayout.NORTH);

		clickSpinner = new JSpinner();
		panel_2.add(clickSpinner, BorderLayout.NORTH);
		panel_1.setLayout(new BorderLayout(0, 0));

		durationSpinner = new JSpinner();
		panel_1.add(durationSpinner);
		panel.setLayout(gl_panel);
		viewsSpinner.setModel(new SpinnerNumberModel(10, 10, 500, 10));
		clickSpinner.setModel(new SpinnerNumberModel(10, 10, 500, 10));
		durationSpinner.setModel(new SpinnerDateModel(new Date(1326409200000L), null, null, Calendar.DAY_OF_YEAR));

		JPanel adPanel = new JPanel();
		adPanel.setBorder(new TitledBorder(null, "Select Ad:", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		add(adPanel, BorderLayout.NORTH);
		adPanel.setLayout(new BorderLayout(0, 0));
		selectAdComboBox = new JComboBox();
		adPanel.add(selectAdComboBox, BorderLayout.CENTER);

		initComboBox();

	}

	private void initComboBox() {
		for (int i = 0; i < ArenaManager.getInstance().getAds().size(); i++) {
			selectAdComboBox.insertItemAt(ArenaManager.getInstance().getAds().get(i).getDescription(), i);
		}
		selectAdComboBox.addActionListener(new cbListener());
	}

	public class cbListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			JComboBox cb = (JComboBox) e.getSource();
			adIndex = cb.getSelectedIndex();
			initAdvertisement(ArenaManager.getInstance().getAds().get(adIndex));
		}
	}

	private void initAdvertisement(Ad ad) {
		tmp = ad;

		viewsSpinner.setValue((Integer) tmp.getScheme().getViews());
		clickSpinner.setValue((Integer) tmp.getScheme().getClicks());
		durationSpinner.setValue(tmp.getScheme().getDuration());

		txtDescription.setText(ad.getDescription());
		lblIcon.setIcon(ad.getImage());
	}

	private void editAdvertisement() {
		tmp.getScheme().setClicks((Integer) clickSpinner.getValue());
		tmp.getScheme().setViews((Integer) viewsSpinner.getValue());
		tmp.getScheme().setDuration((Date) durationSpinner.getValue());
		tmp.setStatus(Config.AdvertiseStatus.PENDING);
		tmp.setDescription(txtDescription.getText());

		ArenaManager.getInstance().editAd(tmp, adIndex);

	}
}
